package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by 18702 on 2017/12/15.
 */
@Controller
@RequestMapping(value = "/user/")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse login (String username , String password, HttpSession session){
        ServerResponse<User> response = userService.login(username, password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    /**
     * 用户登出
     * @param session
     * @return
     */
    @RequestMapping(value = "logout" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse logout ( HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    @RequestMapping(value = "register" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse register(User user){
        return userService.register(user);
    }

    @RequestMapping(value = "check_valid" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkValid(String str,String type){
        return userService.checkValid(str,type);
    }

    @RequestMapping(value = "get_user_info" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> getUserInfo(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user!= null){
            ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorOnMsg("用户信息不存在" );
    }

    @RequestMapping(value = "forget_get_question" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetGetQuestion(String username){
        return userService.selectQuestion(username);
    }

    @RequestMapping(value = "forget_check_answer" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetCheckAnswer(String username ,String password , String answer){
        return userService.checkAnswer(username, password,answer);
    }

    @RequestMapping(value = "forget_rest_password" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetRestPassword(String username,String passwordNew,String forgetToken){
        return userService.forgetRestPassword(username,passwordNew,forgetToken);
    }

    @RequestMapping(value = "rest_password" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld, String passwordNew){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorOnMsg("用户未登录");
        }
        return userService.resetPassword(passwordOld,passwordNew,user);
    }

    @RequestMapping(value = "update_Information" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> updateInformation(HttpSession session,User user){
        User currentuser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentuser == null){
            return ServerResponse.createByErrorOnMsg("用户未登录");
        }
        user.setId(currentuser.getId());
        user.setUsername(currentuser.getUsername());
        ServerResponse<User> userServerResponse = userService.updateInformation(user);
        if(userServerResponse.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,userServerResponse.getData());
        }
        return userServerResponse;
    }

    @RequestMapping(value = "get_information", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> get_information(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户需要登录，状态码为10");
        }
        return userService.getinformation(user.getId());

    }
}
